package com.lin.controller;

import com.lin.VO.ResultVO;
import com.lin.converter.ProductForm2ProductDTOConverter;
import com.lin.dataobject.Product;
import com.lin.dataobject.ProductAttributeValue;
import com.lin.dataobject.dao.ProductDao;
import com.lin.dto.NewProductDTO;
import com.lin.dto.ProductDTO;
import com.lin.enums.ResultEnum;
import com.lin.form.NewProductForm;
import com.lin.server.ProductService;
import com.lin.utils.ResultVOUtils;
import com.lin.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017-10-20.
 */
@Controller
@RequestMapping("/newProduct")
public class SellerNewProductController {
    @Autowired
    private ProductService service;

    @Autowired
    private ProductDao dao;

    @RequestMapping("list")
    public ModelAndView list(){
        return new ModelAndView("product/index2");
    }

    @ResponseBody
    @RequestMapping("/getData")
    public Map<String,Object> getData(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                      @RequestParam(value = "rows", defaultValue = "10") Integer rows,
                                      HttpServletRequest request) {
        PageRequest request1=new PageRequest(page-1,rows);
        String name=request.getParameter("name");
        String startTime=request.getParameter("startTime");
        String endTime=request.getParameter("endTime");
        String id=request.getParameter("id");
        String category=request.getParameter("category");
//        Sort sort=new Sort(Sort.Direction.DESC,"createTime");
//        Page<Product> products=service.findAll(id,name,category,startTime,endTime,request1);
        Map<String,Object> map=new HashMap<>();


        List<ProductDTO> productDTOS=dao.pageList(id,name,category,startTime,endTime,page,rows);
        map.put("rows",productDTOS);
        map.put("total",service.getSize(id,name,category,startTime,endTime));
//        map.put("rows",products.getContent());
//        map.put("total",service.getSize(id,name,category,startTime,endTime));
        return map;
    }

    @RequestMapping("/view")
    public ModelAndView addView(@RequestParam(value = "id",required = false) String id,
                                Map<String,Object> map){
        if (StringUtils.isNotEmpty(id)){
            Product product=service.findOne(id);
            map.put("product",product);
        }
        return new ModelAndView("product/add_view");
    }

    @ResponseBody
    @PostMapping("/save")
    public ResultVO save(@Valid NewProductForm form, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVOUtils.error(ResultEnum.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }

//        Product product=new Product();
//        try{
//            if (StringUtils.isNotEmpty(form.getId())){
//                product=service.findOne(form.getId());
//            }
//            else{
//                form.setId(KeyUtils.getUniqueKey());
//            }
//            BeanUtils.copyProperties(form,product);
//            service.save(product);
//        }catch (SellException e){
//            return ResultVOUtils.error(ResultEnum.SAVE_ERROR.getCode(),
//                    ResultEnum.SAVE_ERROR.getMessage());
//        }
        NewProductDTO newProductDTO= ProductForm2ProductDTOConverter.convert(form);
        if (!StringUtils.isNotEmpty(form.getId())) {
            NewProductDTO result = service.save(newProductDTO);
        }
        else {
            NewProductDTO result=service.update(newProductDTO);
        }
        return ResultVOUtils.success("保存成功");
    }

    @ResponseBody
    @PostMapping("/del")
    public ResultVO del(@RequestParam(value = "id",required = false) String id){
        Product product=service.findOne(id);
        if (product==null){
            return ResultVOUtils.error(ResultEnum.NO_FOUND.getCode(),
                    ResultEnum.NO_FOUND.getMessage());
        }
        service.del(product.getId());
        return ResultVOUtils.success("删除成功");
    }

    @ResponseBody
    @GetMapping("/getAttribute")
    public List<ProductAttributeValue> getAttribute(String productId){
        return service.findAttributeByProduct(productId);
    }
}
